package org.example.repository;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;

import java.util.UUID;

@Aspect
public class RequestAspect {

    private static final String REQUEST_ID = "requestId";

    @Pointcut("@annotation(Request)")
    public void request() {
    }

    @Before("request()")
    public void beforeRequest() {
        MDC.put(REQUEST_ID, nextId());
    }

    @After("request()")
    public void afterRequest() {
        MDC.remove(REQUEST_ID);
    }

    private static String nextId() {
        return UUID.randomUUID().toString();
    }
}
